var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "components/common/base-component", "mobile/services/shoppingCartService", "common", "components/product-info/component", "react", "maishu-ui-toolkit", "components/common/component"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const base_component_1 = require("components/common/base-component");
    const shoppingCartService_1 = require("mobile/services/shoppingCartService");
    const common_1 = require("common");
    const component_1 = require("components/product-info/component");
    const React = require("react");
    const ui = require("maishu-ui-toolkit");
    const component_2 = require("components/common/component");
    let ProductInfoFooter = class ProductInfoFooter extends base_component_1.BaseComponent {
        constructor(props) {
            super(props);
            this.shoppingCart = this.createService(shoppingCartService_1.ShoppingCartService);
            setTimeout(() => {
                let c = (this.masterPage.childComponents[component_1.ProductInfo.name] || [])[0]; //this.mobilePage.controls.filter(o => o instanceof ProductInfoControl)[0] as any as ProductInfoControl;
                if (c == null) {
                    return;
                }
                console.assert(c != null);
                this.state = {
                    productsCount: shoppingCartService_1.ShoppingCartService.productsCount.value,
                    product: c.props.product
                };
                let self = this;
                let componentDidUpdate = c['componentDidUpdate'];
                c['componentDidUpdate'] = function () {
                    // self.state.product = c.state.product;
                    // self.setState(self.state);
                    self.setState({ product: c.props.product });
                    if (componentDidUpdate)
                        componentDidUpdate();
                };
            }, 100);
        }
        get persistentMembers() {
            return [];
        }
        get hasEditor() {
            return false;
        }
        addToShoppingCart() {
            // let c = this.mobilePage.controls.filter(o => o instanceof ProductInfoControl)[0] as any as ProductInfoControl;
            let c = this.masterPage.childComponents[component_1.ProductInfo.name][0];
            let shoppingCart = this.createService(shoppingCartService_1.ShoppingCartService); //this.props.shoppingCart;
            let product = c.props.product;
            let id = product.Id;
            let count;
            let shoppingCartItem = shoppingCartService_1.ShoppingCartService.items.value.filter(o => o.ProductId == product.Id)[0];
            if (shoppingCartItem == null) {
                shoppingCartItem = {
                    Id: common_1.guid(),
                    Amount: product.Price * count,
                    Count: count,
                    ImagePath: product.ImagePath,
                    Name: product.Name,
                    ProductId: product.Id,
                    Selected: true,
                    Price: product.Price,
                };
                count = 1;
            }
            else {
                count = shoppingCartItem.Count + 1;
            }
            return shoppingCart.setItemCount(product, count);
        }
        componentDidMount() {
            common_1.subscribe(this, shoppingCartService_1.ShoppingCartService.productsCount, (value) => {
                this.setState({ productsCount: value });
            });
        }
        render() {
            let { productsCount, product: p } = this.state || {};
            let allowBuy = p != null && (p.Stock == null || p.Stock > 0) && p.OffShelve != true;
            let buttonText = "加入购物车";
            if (p != null && p.Stock == 0) {
                buttonText = "商品已售罄";
            }
            else if (p != null && p.OffShelve == true) {
                buttonText = "商品已下架";
            }
            return (React.createElement("nav", { className: "settlement" },
                React.createElement("a", { href: '#shopping_shoppingCart', className: "pull-left" },
                    React.createElement("i", { className: "icon-shopping-cart" }),
                    productsCount ?
                        React.createElement("span", { className: "badge bg-primary" }, productsCount)
                        : null),
                React.createElement("button", { disabled: !allowBuy, style: { width: 120 }, ref: (e) => e ? e.onclick = ui.buttonOnClick(() => this.addToShoppingCart()) : null, className: "btn btn-primary pull-right" }, buttonText)));
        }
    };
    ProductInfoFooter = __decorate([
        component_2.component()
    ], ProductInfoFooter);
    exports.ProductInfoFooter = ProductInfoFooter;
});
